package view;

import java.util.LinkedList;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JPanel;

import controller.Controller;
import javax.swing.BoxLayout;

@SuppressWarnings("serial")
public class TransactionPanel extends JPanel implements Observer {

	private Controller controller;

	/**
	 * Create the panel.
	 * @param controller 
	 */
	public TransactionPanel(Controller controller) {
		this.controller = controller;
		controller.getModel().addObserver(this);
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		parseTransactions();
	}

	private void parseTransactions() {
		removeAll();
		
		LinkedList<Object> recent = controller.getModel().getDataSource().getRecentTransactions();
		
		for (Object transaction : recent) {
			TransactionItem item = new TransactionItem(transaction, controller);
			add(item);
		}
		
		revalidate();
		repaint();
	}

	@Override
	public void update(Observable o, Object arg) {
		parseTransactions();		
	}

}
